<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 《Javascript设计模式与开发实践》P49 -->
    <script>
        var cost = (function () {
            var args = [];
            return function () {
                if (arguments.length === 0) {
                    var money = 0;
                    for (var i = 0, l = args.length; i < l; i++) {
                        money += args[i];
                    }
                    return money;
                } else {
                    [].push.apply(args, arguments);
                }
            }
        })();
        cost(100);
        cost(200);
        cost(300);
        console.log(cost());
    </script>
    <script>
        var currying = function (fn) {
            var args = [];
            return function () {
                if (arguments.length === 0) {
                    return fn.apply(this, args);
                } else {
                    [].push.apply(args, arguments);
                    return arguments.callee;
                }
            }
        };
        var cost = (function () {
            var money = 0;
            return function () {
                for (var i = 0, l = arguments.length; i < l; i++) {
                    money += arguments[i];
                }
                return money;
            }
        })();

        var cost = currying(cost); // 转化成currying 函数
        cost(100); // 未真正求值
        cost(200); // 未真正求值
        cost(300); // 未真正求值
        alert(cost()); // 求值并输出：600

    </script>
</body>

</html>